package service.impl;

import common.Constants.ServiceConstant;
import common.Result;
import dao.ScoreInfoDao;
import dao.ScoresDao;
import dataBean.ScoreInfoDo;
import dataBean.ScoresDo;
import org.springframework.beans.factory.annotation.Autowired;
import service.ScoreService;

/**
 * Created by lxl on 2015/10/30.
 */
public class ScoreServiceImpl implements ScoreService{

    @Autowired
    private ScoresDao scoresDao ;

    @Autowired
    private ScoreInfoDao scoreInfoDao ;

    @Override
    public Result updateScore(ScoresDo scoresDo, ScoreInfoDo scoreInfoDo) {

        Result result = new Result() ;

        try {

            int scoreId  = scoresDao.selectScoresByKey(scoresDo);

            if( scoreId == 0  ){

                synchronized (scoreInfoDao){

                    scoreInfoDao.insertScoreInfo(scoreInfoDo) ;
                    scoresDo.setScoreID(scoreInfoDao.getLasetID());

                }

                scoresDao.updateScoresByScoreId(scoresDo);

            }else {

                scoreInfoDo.setId(scoreId);

                if( scoreInfoDao.updateScoreInfoById(scoreInfoDo) == 0 ){

                    result.setSucces(false);
                    result.setError(ServiceConstant.SCORE_EXIST_ERROR);

                    return result ;

                }

            }

        }catch (Exception e){

            System.out.println(e.toString());

            result.setSucces(false);
            result.setError(ServiceConstant.UPDATE_SCORE_ERROR);

            return result ;

        }

        result.setSucces(true);

        return result;
    }
}
